package cn.xumaoyang.order.controller;

import cn.xumaoyang.basic.util.AjaxResult;
import cn.xumaoyang.basic.util.LoginContext;
import cn.xumaoyang.basic.util.PageList;
import cn.xumaoyang.order.domain.AdoptOrder;
import cn.xumaoyang.order.query.AdoptOrderQuery;
import cn.xumaoyang.order.service.IAdoptOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 领养订单接口。
 */
@RestController
@RequestMapping("/adopt")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @PostMapping
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try {
            //返回支付的字符串，为了方便支付宝支付。
           String payDate = adoptOrderService.submit(params, LoginContext.getCurrentLoginInfo(request));
            System.out.println(payDate);
            return new AjaxResult().setResultObj(payDate);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"下单失败");
        }
    }

    //订单的查看功能,用户的。
    @PostMapping("/user")
    public PageList<AdoptOrder> user(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        return adoptOrderService.user(query,LoginContext.getCurrentLoginInfo(request));
    }

    //订单的查看功能,管理员的。
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        return adoptOrderService.admin(query,LoginContext.getCurrentLoginInfo(request));
    }


}
